package com.pospi.pospihold.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.pospi.pospihold.R;
import com.pospi.pospihold.dto.GoodsDto;
import com.pospi.pospihold.util.DoubleSave;

import java.util.List;

/**
 * Created by Qiyan on 2016/6/21.
 */
public class RefundGoodsDetailAdapter extends BaseAdapter {
    private Context context;
    private List<GoodsDto> goodsDtos;

    public RefundGoodsDetailAdapter(List<GoodsDto> goodsDtos, Context context) {
        this.goodsDtos = goodsDtos;
        this.context = context;
    }

    @Override
    public int getCount() {
        return goodsDtos.size();
    }

    @Override
    public Object getItem(int position) {
        return goodsDtos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.refund_order_detail_item, null);
            viewHolder = new ViewHolder();
            viewHolder.no = (TextView) convertView.findViewById(R.id.refund_detail_no);
            viewHolder.name = (TextView) convertView.findViewById(R.id.refund_detail_name);
            viewHolder.num = (TextView) convertView.findViewById(R.id.refund_detail_num);
            viewHolder.price = (TextView) convertView.findViewById(R.id.refund_detail_price);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.no.setText(String.valueOf(position + 1));
        viewHolder.name.setText(goodsDtos.get(position).getName());
        viewHolder.num.setText(goodsDtos.get(position).getNum() + "*" + goodsDtos.get(position).getPrice());
        double money = goodsDtos.get(position).getNum() * goodsDtos.get(position).getPrice();
        viewHolder.price.setText(String.valueOf(DoubleSave.doubleSaveTwo(money)));

        return convertView;
    }

    class ViewHolder {
        TextView no;
        TextView name;
        TextView num;
        TextView price;
    }
}
